package com.cuteboy.util;

import java.util.Random;

/**
 * Created by Administrator on 2016/1/20.
 */
public class RandomStringUtils {
    /** 纯数字 */
    public static final int ALL_NUMBER = 1;
    /** 纯小写字母 */
    public static final int ALL_LOWERCASE = 2;
    /** 纯大写字母 */
    public static final int ALL_CAPITAL = 3;
    /** 纯字母 */
    public static final int ALL_LETTER = 4;
    /** 字母数字混合 */
    public static final int LETTER_NUMBER = 7;

    private static Random RANDOM = new Random();

    public static String randomStr(int type,int length) {
        String resultStr = null;
        switch (type) {
            case ALL_NUMBER:
                resultStr = randomStr("1234567890",length);
                break;
            case ALL_LOWERCASE:
                resultStr = randomStr("qwertyuiopasdfghjklzxcvbnm",length);
                break;
            case ALL_CAPITAL:
                resultStr = randomStr("QWERTYUIOPASDFGHJKLZXCVBNM",length);
                break;
            case ALL_LETTER:
                resultStr = randomStr("qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM",length);
                break;
            case LETTER_NUMBER:
            default:
                resultStr = randomStr("1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM",length);
        }
        return resultStr;
    }

    public static String randomStr(String randString,int length) {
        if(length < 0 || randString == null) {
            return null;
        }
        String resultStr = "";
        int len = randString.length();
        for(int i=0;i<length;++i) {
            resultStr += randString.charAt(RANDOM.nextInt(len));
        }
        return resultStr;
    }
}
